package com.javafactroy.kunlong.data.service;


import com.javafactroy.kunlong.base.dto.TQueryParam;
import com.javafactroy.kunlong.base.model.ReturnPageInfo;
import com.javafactroy.kunlong.client.feign.dto.PageInfoDto;
import com.javafactroy.kunlong.data.dao.GeneralSqlMapper;
import com.javafactroy.kunlong.data.factroy.GenericParamFactroy;
import com.javafactroy.kunlong.data.iface.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Slf4j
public class ApiService<DTO extends T, T extends PageInfoDto, M extends GeneralSqlMapper<T>>
        implements IGeneralService<DTO> {
    @Autowired
    GenericParamFactroy genericParamFactroy;

    //@Autowired
    IGenericParam<T> genricParam;

    public IquerySome<DTO> iquerySome = (DTO dto) -> {
    };

    public IquerySomeList<DTO> iquerySomeList = (List<DTO> list) -> {
    };
    @Autowired
    IentityConvert<DTO, T> convert;
    @Autowired
    M mapper;

    public int save(DTO entity) {

        if(entity.checkInsertFlag()) {
            return insert(entity);
        }
        return update(entity);
    }

    public int saveBatch(List<DTO> entities) {
        for (T e : entities) {
            mapper.insert(e);
        }
        return 0;
    }


    public int insert(DTO entity) {
        return mapper.insert(entity);
    }

    public int update(DTO entity) {
        return mapper.updateByPrimaryKey(entity);
    }

    public int updateNotNull(DTO entity) {
        return mapper.updateByPrimaryKeySelective(entity);
    }

    public int deleteByPkeys(DTO entity) {
        return mapper.deleteByPrimaryKey(entity);
    }

    public int deleteByPkey(Long id) {
        return mapper.deleteByPrimaryKey(id);
    }

    public void deleteBatchByPkey(List<Long> ids) {
        ids.forEach(i -> mapper.deleteByPrimaryKey(i));
    }

    public int deleteBy(DTO entity) {
        return mapper.delete(entity);
    }

    public int selectCount(DTO entity) {
        return mapper.selectCount(entity);
    }

    public DTO selectByPkey(Long pkey) {
        DTO dto = convert.toOne(mapper.selectByPrimaryKey(pkey));
        iquerySome.querySome(dto);
        return dto;
    }

    @Override
    public DTO selectByPkeys(DTO dto) {
        return convert.toOne(mapper.selectByPrimaryKey(dto));
    }

    public List<DTO> select(DTO e) {
        e.startPage();

        return convert.toList(mapper.select(e));
    }

    public List<DTO> selectAll() {
        return convert.toList(mapper.selectAll());
    }

    public ReturnPageInfo<DTO> queryParam(@Param("queryParam") TQueryParam<DTO> queryParam) {


        return query(queryParam.toParam());

    }

    public ReturnPageInfo<DTO> query(@Param("queryParam") DTO dto) {

        //entity.fields2Col();        //entity.startOrder();        //entity.startPage();
        dto.startPageOrder();
        List<T> list = mapper.query(dto);

        ReturnPageInfo<T> apiPageInfo = new ReturnPageInfo<>();
        apiPageInfo.setPageSize(dto.getPageSize());
        apiPageInfo.setPageNum(dto.getPageNum());
        apiPageInfo.valueOf(list);
        List<DTO> dtos = convert.toList(list);
        iquerySomeList.querySome(dtos);
        return new ReturnPageInfo<DTO>().valueOf(apiPageInfo).setList(dtos);
    }

    public long count(@Param("queryParam") DTO dto) {
        dto.fields2Col();
        return mapper.count(dto);
    }

    public void initSome(long parentId) {
        iquerySome = (DTO dto) -> {

        };

        iquerySomeList = (List<DTO> list) -> {
            list.forEach(i -> iquerySome.querySome(i));
        };
    }


}
